package com.yesep.learn.design.structural.composite;

import java.util.*;

/**
 * 实现 Composite 类
 */
public class Directory implements IFileSystemComponent {
    private String name;
    private List<IFileSystemComponent> components = new ArrayList<>();

    public Directory(String name) {
        this.name = name;
    }

    @Override
    public void add(IFileSystemComponent component) {
        components.add(component);
    }

    @Override
    public void remove(IFileSystemComponent component) {
        components.remove(component);
    }

    @Override
    public IFileSystemComponent getChild(int index) {
        return components.get(index);
    }

    @Override
    public void display(int depth) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < depth; i++) {
            sb.append("-");
        }
        System.out.println(sb.toString() + "Directory: " + name);

        for (IFileSystemComponent component : components) {
            component.display(depth + 2);
        }
    }
}